/**
 * Created by hjx on 2018/2/8.
 */
(function () {
    var exports = this;

    var $ = null;

    var javaClubInputLayer = cc.Layer.extendEX({
        layerHeight: 0,
        input_num: '',
        ctor: function (type, data, callback) {
            this._super();
            var that = this;
            if (type == 'changeName') { //input输入
                loadNodeCCS(res.ClubChangeNameLayer_json, this);
                $ = create$(this.getChildByName('Layer'));
                $('lb_content').setVisible(false);
                $('lb_content').setString('请输入要修改的亲友圈名称');

                $('input').setTextVerticalAlignment(cc.VERTICAL_TEXT_ALIGNMENT_CENTER);
                $('input').addEventListener(function (textField, type) {
                    switch (type) {
                        case ccui.TextField.EVENT_ATTACH_WITH_IME:
                            cc.log('attach with IME');
                            break;
                        case ccui.TextField.EVENT_DETACH_WITH_IME:
                            that.setPositionY(0);
                            cc.log('detach with IME');
                            break;
                        case ccui.TextField.EVENT_INSERT_TEXT:
                            cc.log('insert words');
                            break;
                        case ccui.TextField.EVENT_DELETE_BACKWARD:
                            cc.log('delete word');
                            break;
                        default:
                            break;
                    }
                }, that);
                safeString($('input'), '');
            } else { //按钮输入
                if (type == 'invite') {
                    loadNodeCCS(res.ClubInputLayer_json, this);
                    $ = create$(this.getChildByName('Layer'));
                    safeShow($('word_invite'));
                    safeHide($('word_join_club'));
                    safeString($('id_des'), '请输入玩家ID:');
                    $('id_des').ignoreContentAdaptWithSize(true);
                    safeString($('input'), '');
                } else if (type == 'join' || type == 'changePower' || type == 'setPower') {
                    loadNodeCCS(res.ClubInputLayer_json, this);
                    $ = create$(this.getChildByName('Layer'));
                    safeHide($('word_invite'));
                    safeShow($('word_join_club'));
                    safeString($('id_des'), '请输入亲友圈ID:');
                    $('id_des').ignoreContentAdaptWithSize(true);
                    if (data) {
                        $('input').setString('' + data);
                    }else{
                        safeString($('input'), '');
                    }
                }

                for (var i = 0; i <= 9; i++) {
                    (function (i) {
                        TouchUtil.setOnclickListener($('' + i), function () {
                            if (that.input_num.length < 10) {
                                that.input_num = that.input_num + i;
                                $('input').setString(that.input_num);
                            }
                        });
                    })(i);
                }
                TouchUtil.setOnclickListener($('btn_reset'), function () {
                    that.input_num = '';
                    $('input').setString(that.input_num);
                });

                TouchUtil.setOnclickListener($('btn_delete'), function () {
                    that.input_num = that.input_num.substring(0, that.input_num.length - 1);
                    $('input').setString(that.input_num);
                });
            }
           
                

            TouchUtil.setOnclickListener($('btn_close'), function () {
                that.removeFromParent();
            });
            TouchUtil.setOnclickListener($('root'), function () {
            });

            TouchUtil.setOnclickListener($('btn_ok'), function () {
                if (type == 'changeName') {
                    $('input').didNotSelectSelf();
                }
                var input = $('input').getString();

                if (type == 'apply') {
                    if (input == null || input == undefined || input == '') {
                        var tip = '亲友圈ID不能为空';
                        if (that.createoradd == 'create') {
                            tip = '亲友圈名称不能为空';
                        }
                        showClubAlert(tip);
                        return;
                    }
                    if (input.length < 1 || input.length > 17) {
                        var tip = '群ID长度在1-16字符之间';
                        if (that.createoradd == 'create') {
                            tip = '群昵称长度在1-16字符之间';
                        }
                        showClubAlert(tip);
                        return;
                    }

                    var inputnum = parseInt(input);
                    if (!inputnum) {
                        showClubAlert('群ID不是数字！');
                        return;
                    }
                    var name = gameData.nickname;
                    name = name.replace('%', '').replace('\'', '').replace('"', '').replace('\\', '');
                    network.send(2103, {
                        cmd: 'applyClub',
                        club_id: input,
                        name: name,
                        head: gameData.headimgurl
                    });
                } else if (type == 'invite') {
                    var inputnum = parseInt(input);
                    if (!inputnum) {
                        showClubAlert('玩家ID不是数字!');
                        return;
                    }
                    network.send(2103, {cmd: 'inviteClub', club_id: data['_id'], obj_id: inputnum});
                } else if (type == 'changeName') {
                    var tip = '';
                    var str = input;
                    if (str == null || str == undefined || str == '') {
                        tip = '亲友圈名称为空';
                    }
                    if (tip == '' && (str.length < 2 || str.length > 17)) {
                        tip = '群名称长度在2-16字符之间';
                    }
                    if (tip == '') {
                        network.send(2103, {cmd: 'modifyClub', club_id: data['_id'], name: str});
                        that.removeFromParent();
                    } else {
                        showClubAlert(tip);
                    }
                } else if (type == 'join') {
                    if (input == null || input == undefined || input == '') {
                        var tip = '亲友圈ID不能为空';
                        showClubAlert(tip);
                        return;
                    }
                    if (input.length < 1 || input.length > 17) {
                        var tip = '群ID长度在1-16字符之间';
                        if (that.createoradd == 'create') tip = '群昵称长度在1-16字符之间';
                        showClubAlert(tip);
                        return;
                    }

                    var inputnum = new Number(input);
                    if (_.isNaN(inputnum)) {
                        showClubAlert('请输入正确的群ID（数字）');
                        return;
                    }
                    var name = gameData.nickname;
                    name = name.replace('%', '').replace('\'', '').replace('"', '').replace('\\', '');
                    network.send(clubMsg.joinClub,{club_id:input,name: name,head: gameData.headimgurl});
                    if(clubMsg.Debug){
                        network.recv(clubMsg.msg_8003);
                    }

                }else if(type == 'changePower'){
                    if(input == null || input == undefined || input == ''){
                        showAlert('输入为空');
                        return;
                    }
                    if(callback)  callback(input);
                    that.removeFromParent();
                } else if(type == 'setPower'){
                    if(input == null || input == undefined || input == ''){
                        showAlert('输入为空');
                        return;
                    }
                    if(callback)  callback(input);
                    that.removeFromParent();
                }

            });

            return true;
        },
        getLayerHeight: function () {
            return this.layerHeight;
        },
        onEnter: function () {
            cc.Layer.prototype.onEnter.call(this);
            var that = this;
            that.msgList = [];
            this.list1 = cc.eventManager.addCustomListener('applyClub', function (event) {
                var data = event.getUserData();
                if (data.result == 0) {
                    showClubAlert(data.msg);
                }
                that.removeFromParent();
            });

            this.list3 = cc.eventManager.addCustomListener('inviteClub', function (event) {
                var data = event.getUserData();
                if (data.result == 0) {
                    if (data.msg == '' || data.msg == undefined) {
                        showClubAlert('邀请加入成功!');
                    } else {
                        showClubAlert(data.msg);
                    }
                }
                that.removeFromParent();
            });

        },
        onExit: function () {
            cc.Layer.prototype.onExit.call(this);
            cc.eventManager.removeListener(this.list1);
            cc.eventManager.removeListener(this.list3);
        }
    });

    exports.javaClubInputLayer = javaClubInputLayer;
})(window);